package com.lcredu.sqlSeesion;

import com.lcredu.config.Function;
import com.lcredu.config.MapperBean;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author 黎长容
 * @date 2025-07-28 16:16
 * 读取配置信息,获取连接
 * 读取 XxxMapper.xml，能够创建 MappperBean对象
 */

public class LcrConfiguration {
    //1.获取类加载器
    private static ClassLoader loader = ClassLoader.getSystemClassLoader();

    /**
     * 处理xml信息,返回连接
     */
    public Connection build(String resource) {
        Connection connection = null;
        try {
            InputStream stream = loader.getResourceAsStream(resource);
            SAXReader saxReader = new SAXReader();
            Document document = saxReader.read(stream);
            Element rootElement = document.getRootElement();
            System.out.println(rootElement);
            connection = getConnection(rootElement);
        } catch (DocumentException e) {
            throw new RuntimeException(e);
        }
        return connection;
    }

    //获取连接的方法
    public Connection getConnection(Element element) {
        String driveClass = null;
        String url = null;
        String username = null;
        String password = null;
        //对根节点下面的节点进行解析
        List elements = element.elements();
        for (Object item : elements) {
            Element i = (Element) item;
            String name = i.attributeValue("name");
            String value = i.attributeValue("value");

            switch (name) {
                case "driveClass":
                    driveClass = value;
                    break;
                case "url":
                    url = value;
                    break;
                case "username":
                    username = value;
                    break;
                case "password":
                    password = value;
                    break;
                default:
                    throw new RuntimeException("xml未匹配到相对应的信息");
            }
        }

        try {
            Connection connection = DriverManager.getConnection(url, username, password);
            return connection;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    //在 HspConfiguration, 读取 XxxMapper.xml，能够创建 MappperBean对象
    public MapperBean readMapper(String path) {
        MapperBean mapperBean = new MapperBean();

        try {
            InputStream stream = loader.getResourceAsStream(path);
            SAXReader saxReader = new SAXReader();
            Document document = saxReader.read(stream);
            Element root = document.getRootElement();
//            System.out.println(root);
            String interfaceName = root.attributeValue("namespace").trim();
            mapperBean.setInterfaceName(interfaceName);
            ArrayList<Function> functions = new ArrayList<>();
            //循环遍历
            Iterator iterator = root.elementIterator();
            //开始遍历
            /*
                <select id="getMonsterById" resultType="com.lcredu.entity.Monster">
                      select * from monster where id = ?
                </select>
             */
            while (iterator.hasNext()) {
                Function function = new Function();
                Element element = (Element) iterator.next();
                String sqlType = element.getName().trim();
                String methodName = element.attributeValue("id").trim();
                String resultType = element.attributeValue("resultType").trim();
                String sql = element.getText();
                function.setSql(sql);
                function.setMethodType(sqlType);
                function.setMethodName(methodName);
                function.setResultType(resultType);
                functions.add(function);
                mapperBean.setFunctions(functions);
            }
        } catch (DocumentException e) {
            throw new RuntimeException(e);
        }

        return mapperBean;
    }
}
